import cv2
import numpy as np
from abc import ABC, abstractmethod
# 抽象部分 - 图像格式
class ImageFormat:
    def __init__(self, filter):
        self.filter = filter
    
    def set_filter(self, filter):
        self.filter = filter
    
    @abstractmethod
    def process_image(self, file_path):
        pass

# 具体图像格式
class JPGFormat(ImageFormat):
    def process_image(self, file_path):
        image = cv2.imread(file_path)
        if image is None:
            raise ValueError("无法读取JPG图像")
        processed = self.filter.apply_filter(image)
        cv2.imshow('Processed JPG', processed)
        cv2.waitKey(0)
        return processed

class GIFFormat(ImageFormat):
    def process_image(self, file_path):
        cap = cv2.VideoCapture(file_path)
        ret, frame = cap.read()
        if not ret:
            raise ValueError("无法读取GIF图像")
        
        processed_frames = []
        while ret:
            processed = self.filter.apply_filter(frame)
            processed_frames.append(processed)
            ret, frame = cap.read()
        
        # 显示第一帧作为示例
        cv2.imshow('Processed GIF (first frame)', processed_frames[0])
        cv2.waitKey(0)
        cap.release()
        return processed_frames

class BMPFormat(ImageFormat):
    def process_image(self, file_path):
        image = cv2.imread(file_path)
        if image is None:
            raise ValueError("无法读取BMP图像")
        processed = self.filter.apply_filter(image)
        cv2.imshow('Processed BMP', processed)
        cv2.waitKey(0)
        return processed